package com.maxatime.web.controller;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import org.apache.poi.ss.usermodel.Cell;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName Excel
 * @Description ssb读取excel文件，将用户数据放到一个List中
 * @Author saxas
 * @Date 2024/7/29 9:50
 **/
public class Excel {
    public static void main(String[] args) {
        String filePath = "C:\\Users\\saxas\\Desktop\\工作簿1.xlsx"; // 替换为您的 Excel 文件路径
        List<String> userDataList = readExcelData(filePath);
        for (String data : userDataList) {
            System.out.println(data);
        }
    }

    public static List<String> readExcelData(String filePath) {
        List<String> dataList = new ArrayList<>();
        try (FileInputStream fileInputStream = new FileInputStream(filePath);
             Workbook workbook = new XSSFWorkbook(fileInputStream)) {
            Sheet sheet = workbook.getSheetAt(0);
            for (Row row : sheet) {
                for (Cell cell : row) {
                    dataList.add(cell.toString());//使用cell.getStringCellValue()方法报错就用cell.toString()
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return dataList;
    }
}